/**
 * 
 */
package officeserver.office.network;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author Chris Bayruns
 * 
 */
public class RequestQueue {
    private static BlockingQueue<RequestQueueWrapper> rQueue;
    private static boolean working = false;

    private static void initQueue() {
	rQueue = new LinkedBlockingQueue<RequestQueueWrapper>();
	// rQueue = new LinkedList<RequestQueueWrapper>();
    }

    public static void enqueue(RequestQueueWrapper in) {
	if (rQueue == null) {
	    initQueue();
	}

	try {
	    rQueue.put(in);
	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	// System.out.println("Request Queue Size: " + rQueue.size());
    }

    public static boolean hasWork() {
	if (rQueue == null) {
	    initQueue();
	}

	return !rQueue.isEmpty() && !working;
    }

    public static void doWork() {
	RequestQueueWrapper node = null;
	if (rQueue == null) {
	    initQueue();
	}

	if (hasWork()) {
	    try {
		node = rQueue.take();
	    } catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }

	    if (node != null && node.getRequest() != null) {
		working = true;
		node.processRequest();
		working = false;
	    }
	}
    }
}
